﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;
using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementLineSelect : OperationImplement
{
    Vector3Int rectangleOldPosition;
    List<Vector3Int> oldSerializeVoxel = new List<Vector3Int>();
    VoxelCategories.OperationSelectOperator selectOperator;

    Vector3Int[] zeroVector3Int = new Vector3Int[0];

    //public OperationImplementLineSelect(ProxyOperation proxy) : base(proxy)
    //{

    //}

    public override void Start(StartArg arg)
    {
        //this.nowDrawVoxel3DCore = arg.nowDrawVoxel3DCore;
        base.Start(arg);

        this.selectOperator = arg.selectOperator;
    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            rectangleOldPosition = arg.nowSelectPosition;

            var arrayPoint = BasicAlgorithm.GetLineBlock3D(arg.gestureStartPosition, rectangleOldPosition);


            switch (selectOperator)
            {
                case VoxelCategories.OperationSelectOperator.Union:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 255);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 1);
                    break;
                case VoxelCategories.OperationSelectOperator.Remove:
                    nowDrawVoxel3DCore.DrawSelect(oldSerializeVoxel, 1);
                    oldSerializeVoxel = nowDrawVoxel3DCore.DrawSelect(arrayPoint, 255);
                    break;
                default:
                    break;
            }

            //nowDrawVoxel3DCore.DrawPaintBlock(historyRecordFrontList);
            //var data = nowDrawVoxel3DCore.DrawPaintBlock(arrayPoint, arg.nowSelectUVKey);
            //historyRecordFrontList = data.Item1;
            //historyRecordBreakList = data.Item2;
        }
    }

    public override void Over(OverArg arg)
    {
        switch (selectOperator)
        {
            case VoxelCategories.OperationSelectOperator.Union:
                //foreach (var item in oldSerializeVoxel)
                //{
                //    allSelectVoxel[item.GetHashCode()] = item;
                //}
                proxy.Write(VoxelCategories.Operation.SelectAdd, zeroVector3Int, oldSerializeVoxel);
                oldSerializeVoxel = new List<Vector3Int>();
                break;
            case VoxelCategories.OperationSelectOperator.Remove:
                //Dictionary<Vector3Int, Vector3Int> oldAllSelectVoxel = new Dictionary<Vector3Int, Vector3Int>(allSelectVoxel.Count);
                //foreach (var item in allSelectVoxel)
                //{
                //    oldAllSelectVoxel[item] = item;
                //}
                //foreach (var item in oldSerializeVoxel)
                //{
                //    oldAllSelectVoxel.Remove(item);
                //}
                //allSelectVoxel.Clear();
                //allSelectVoxel.AddRange(oldAllSelectVoxel.Keys);
                //oldAllSelectVoxel.Clear();
                //foreach (var item in oldSerializeVoxel)
                //{
                //    allSelectVoxel.Remove(item.GetHashCode());
                //}
                proxy.Write(VoxelCategories.Operation.SelectRemove, oldSerializeVoxel, zeroVector3Int);
                oldSerializeVoxel = new List<Vector3Int>();
                break;
            default:
                break;
        }
    }



}
